# -*- coding:utf-8 -*-

"""
    对已存在的zip包资源描述进行编辑
"""

import tornado.web
from service import base
from configure import config
from utils.routes import route
from utils import log as logger
from utils import mongo


@route('/manage/edit/?', 'manage.edit')
class EditHandler(base.BaseWebHandler):
    """ 新增设备
    """
    db_name = config.DB_NAME
    collection_name = config.COLLECTION_DATAS

    @tornado.web.asynchronous
    @tornado.gen.engine
    def get(self, *argl, **argd):
        _id = self.get_argument('_id', None)
        
        logger.debug( '_id =', _id, caller=self)
        if not _id:
            self.finish('params error!')
            return

        spec = {'_id': _id}
        datas = yield tornado.gen.Task(mongo.find_one, self.db_name, self.collection_name, spec)
        if datas:
            self.render('manage/edit.html', datas=datas)
        else:
            self.finish('can not find datas!')

    @tornado.web.asynchronous
    @tornado.gen.engine
    def post(self, *argl, **argd):
        _id = self.get_argument('_id', None)
        finger_point = self.get_argument('finger_point', None)
        change_logs = self.get_argument('change_logs', None)
        
        logger.debug('_id =', _id, 'finger_point =', finger_point, 'change_logs =', change_logs, caller=self)
        
        if not _id or not finger_point or not change_logs:
            self.finish({'info': '参数错误!', 'code': 1})
            return
        
        spec = {'_id': _id}
        field = {'finger_point': finger_point, 'info': change_logs}
        r = yield tornado.gen.Task(mongo.update, self.db_name, self.collection_name, spec, field)
        if r:
            self.finish({'info': '更新成功!', 'code': 0})
        else:
            self.finish({'info': '更新失败!', 'code': 2})
